<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h4>Book Detail Page</h4>
	Book:<%=request.getParameter("book") %>
	<br>
	<br>
	<a href="books.jsp">Return</a>
	<%
			String book=request.getParameter("book");
			Cookie[] cookies=request.getCookies();
			List<Cookie> bookCookies=new ArrayList<Cookie>();
			Cookie tmpCookie=null;
			if(cookies!=null&& cookies.length>0){
				for(Cookie c: cookies){
					String cookieName=c.getName();
					if(cookieName.startsWith("ZY_BOOK_")){
						bookCookies.add(c);
						if(c.getValue().equals(book)){
							tmpCookie=c;
						}
						
					}
				}
				
			}
			
			if(bookCookies.size()>=3&&tmpCookie==null){
				tmpCookie=bookCookies.get(0);
			}
			if(tmpCookie!=null){
				tmpCookie.setMaxAge(0);
				response.addCookie(tmpCookie);
			}
			Cookie cookie=new Cookie("ZY_BOOK_"+book,book);
			response.addCookie(cookie);
	%>
</body>
</html>